package edu.hm.vss.pingpong;


import java.net.*; 
import java.io.*;
public class USocketsClient {
	USocketsClient(String host, int port,int count){
			
		   String ping = "ping";
		   String pong = "pong";
		   String smsg = ping;
		   
		byte[] msg = new byte[1024];
		try {
      	
				DatagramSocket csock = new DatagramSocket(); // nimm belieb. Socket
				String         server_host = host;
				InetAddress    server_adr  = InetAddress.getByName(server_host);
				int            server_port = port;
				msg                        = smsg.getBytes();
				
				while(count>0 ){
				
					DatagramPacket request     = new DatagramPacket(msg, msg.length,
                                                      server_adr, server_port);
					csock.send(request);

					DatagramPacket reply = new DatagramPacket(msg, msg.length);
					csock.receive(reply);
					String recvMsg = new String(msg);
					if(recvMsg==pong)
						System.out.println("got: " + new String(reply.getData()));
					else
						System.out.println("error !");
				}
				
				csock.close();	
		}
		catch (Exception e){ // hier entsprechendes Exception-Handling 
		} 
  }
} 